//
//  TopSpecialDetailsPageTableViewController.m
//  WisdomZhuHai
//
//  Created by BABLE on 15/4/26.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "TopSpecialDetailsPageTableViewController.h"
#import "ProtocolDefine.h"
#import "TransforController.h"
#import "AllSpecialListTableViewController.h"
#import "Common.h"
#import "UIWebControl.h"

//设定CELL高度
const float TOPIMAGECELL_HEIGHT = 143;//顶部图片高(和xib一致)
const float TITLECELL_HEIGHT = 35;//标题高
const float OKCOMMENDCELL_HEIGHT = 85;//专题列表高

@interface TopSpecialDetailsPageTableViewController ()
@property (nonatomic,strong)AllSpecialListTableViewController *moreSpecial;
@property (nonatomic,strong)WebIOS *web;
@end

@implementation TopSpecialDetailsPageTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"专题";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [[ProtocolManager shareInstance] getTopSpecailDetailsWithTopicId:[self.topicId integerValue] target:self selector:@selector(didFinishGetTopSpecailsList:)];
    
    UIView *topBar = [TransforController shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:0 andTitle:@""];
    [self.view addSubview:topBar];

    


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if(indexPath.row == 0){
        NSDictionary *dic = [[NSDictionary alloc] initWithDictionary: self.apiDataDic];
        static NSString *pageCellIdentifier = @"topImageCell";
        cell = [tableView dequeueReusableCellWithIdentifier:@"CommonBigImageCell"];
        // Configure the cell...
        if(cell == nil)
        {
            cell = [[NSBundle mainBundle] loadNibNamed:@"CommonBigImageCell"  owner:self options:nil][0];
        }
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIImageView *topImage = (UIImageView*)[cell viewWithTag:100001];
        [topImage setImageWithURL:[Common getFullDownloadPathByRelativePath:[dic objectForKey:P_TOPIC_DETAILS_TOPIMAGEURL]]];
    }
    else if(indexPath.row == 1){
        static NSString *pageCellIdentifier = @"titleLabelCell";
        cell = [tableView dequeueReusableCellWithIdentifier:pageCellIdentifier];
        // Configure the cell...
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pageCellIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView *backGround = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, TITLECELL_HEIGHT)];
//        backGround.backgroundColor = [UIColor grayColor];
        UILabel *title = [[UILabel alloc] init];
        [title setNumberOfLines:1];
//        [title sizeToFit];
        CGRect frame = title.frame;
//        frame = CGRectMake(10, 0, cell.bounds.size.width, cell.bounds.size.height-17);
        frame =  CGRectMake(3, 0, backGround.bounds.size.width, backGround.bounds.size.height-5);
        title.frame = frame;
        title.font = [UIFont systemFontOfSize:14.0];
        title.text = self.topicTitle;

        [backGround addSubview:title];
        [cell addSubview:backGround];

    }else{
            cell = [tableView dequeueReusableCellWithIdentifier:@"CommonCell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CommonCell"  owner:self options:nil][0];
            }
    
            [self specailListCellSetup:cell index:indexPath.row - 2];
    }
    NSAssert(cell != nil, @"TopSpecialDetailsPageTableViewController cell is nil");
    return cell;
}

#pragma mark - cell
- (void)specailListCellSetup:(UITableViewCell*)cell index:(NSInteger)index;
{
    cell.contentView.backgroundColor = [Common colorWithRGB:0xf9f9f9];
    if (self.newsList.count <= 0) {
        return;
    }
    NSDictionary *cellDic = self.newsList[index];
    NSString *imgUrl = [Common valueForKey:P_NPHOTO dic:cellDic];
    NSString *title = [Common valueForKey:P_NRTITLE dic:cellDic];
    NSArray *tagsList = [Common valueForKey:P_TAGS dic:cellDic];
    
    NSString *source_name = [Common valueForKey:P_V2_SOURCE_NAME dic:cellDic];
    NSString *comment_cnt = [Common valueForKey:P_V2_COMMENT_CNT dic:cellDic];
    NSString *date = [Common valueForKey:P_UPDATED_TIME dic:cellDic];
    
    UIImageView *image = (UIImageView*)[cell viewWithTag:1001];
    [image setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl]];
    UIImageView *okImage = (UIImageView*)[cell viewWithTag:1007];
    okImage.hidden = YES;
    
    UILabel *titleLab = (UILabel*)[cell viewWithTag:1002];
    [titleLab setNumberOfLines:2];
    titleLab.text = title;
    [titleLab sizeToFit];
    CGRect frame = titleLab.frame;
    frame.size.width = 216;
    titleLab.frame = frame;
    
    [self tagSetup:tagsList titleLabHeight:frame.size.height cell:cell];
    UILabel *price = (UILabel*)[cell viewWithTag:1005];
    [price removeFromSuperview];
    UIImageView *districtNameImg = (UIImageView*)[cell viewWithTag:2001];
    [districtNameImg removeFromSuperview];
    UILabel *districtNameLab = (UILabel*)[cell viewWithTag:2002];
    districtNameLab.text = [NSString stringWithFormat:@"%@ %@", [date componentsSeparatedByString:@" "][0],source_name ];
    UIImageView* goodImg = (UIImageView*)[cell viewWithTag:2003];
    [goodImg removeFromSuperview];
    UILabel* CommontLab = (UILabel*)[cell viewWithTag:2006];
    CommontLab.text = comment_cnt;
    
    
}

//标签描绘
- (void)tagSetup:(NSArray*)tagsList titleLabHeight:(CGFloat)height cell:(UITableViewCell*)cell
{
    if(tagsList.count <= 0)
    {
        return;
    }
    CGFloat tagX = 0.0;
    for (int i = 0; i < [tagsList count]; i++) {
        UIView* tagListView = nil;
        if (height < 20.0) {
            tagListView = [cell viewWithTag:1003];
        } else {
            tagListView = [cell viewWithTag:1004];
        }
        NSDictionary *tagDic = tagsList[i];
        NSArray *rgbArray = [[Common valueForKey:P_V2_RGB dic:tagDic] componentsSeparatedByString:@"#"];
        NSString *rgb = nil;
        if ( rgbArray.count < 2) {
            rgb = [NSString stringWithFormat:@"%@%@", @"0x",@"18aaf6"];
        } else {
            rgb = [NSString stringWithFormat:@"%@%@", @"0x",rgbArray[1]];
        }
        NSString *name = [Common valueForKey:P_NAME dic:tagDic];
        UIView *tagView = [[UIView alloc] initWithFrame:CGRectMake(tagX, 0, 45, 16)];
        tagView.layer.cornerRadius = 3.0;
        unsigned int color = strtoul([rgb UTF8String],0,0);//nsstring 转 rgb
        tagView.backgroundColor = [Common colorWithRGB:color];
        UILabel *tagNameLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tagView.frame.size.width, tagView.frame.size.height)];
        tagNameLab.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:10.0];
        tagNameLab.textAlignment = UITextAlignmentCenter;
        tagNameLab.textColor = [UIColor whiteColor];
        tagNameLab.text = name;
        [tagView addSubview:tagNameLab];
        [tagListView addSubview:tagView];
        tagX += 50;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{   //CELL尺寸判断器
    
    if(indexPath.row == 0)
    {
        return TOPIMAGECELL_HEIGHT;
    }
    else if(indexPath.row == 1)
    {
        //专题标题
        return TITLECELL_HEIGHT;
    }
    else
    {
        return OKCOMMENDCELL_HEIGHT;
    }
    return tableView.rowHeight;
}


-(void)didFinishGetTopSpecailsList:(NSDictionary *)dic
{
    if(dic == nil)
    {
        NSLog(@"didFinishTopGetSpecailsList dic is nil");
        return ;
    }
    NSLog(@"didFinishGetTopSpecailsList dic %@",dic);
    self.apiDataDic = [dic objectForKey:@"topic" ];
    self.newsList = [self.apiDataDic objectForKey:P_TPOIC_DETAILS_NEWS];
    self.topicTitle = [self.apiDataDic objectForKey:P_TOPIC_DETAILS_SUBTITLE];
    
    if(self.allSpecailsDisShow != [[NSNumber alloc] initWithInt:1]){
        UIBarButtonItem *moreSpecialItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:@selector(loadInMoreSpecial)];
        moreSpecialItem.image = [UIImage imageNamed:@"More@2x.png"];
        self.navigationItem.rightBarButtonItem = moreSpecialItem;
    }
    
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 65, self.view.bounds.size.width, self.view.bounds.size.height-65) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
    UIView *topBar = [TransforController shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:0 andTitle:@""];
    [self.view addSubview:topBar];

}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //单个资讯详情
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row >= self.newsList.count) {
        return;
    }
    if (indexPath.row > 1) {
        NSDictionary* dic = self.newsList[indexPath.row - 2];
        NSString *url = [Common valueForKey:P_TOPIC_DETAILS_URL dic:dic];
        self.web = [[WebIOS alloc]initWithFrame:[Common shareInstance].contentFrameNoTabBar url:[Common getUrlByString:url]];
        self.web.hidesBottomBarWhenPushed = YES;
        self.web.target_id = [[Common valueForKey:@"id" dic:dic] intValue];
        self.web.type = @"NEWS";
        
        [self.navigationController pushViewController:self.web animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.newsList.count + 2;//设置行数
}

-(void)loadInMoreSpecial{
    self.moreSpecial = [[AllSpecialListTableViewController alloc] init];
    self.moreSpecial.navController = self.navigationController;
    [self.navigationController pushViewController:self.moreSpecial animated:YES];
    
}

- (void)pullTableViewDidTriggerRefresh:(PullTableView*)pullTableView{
    
}
- (void)pullTableViewDidTriggerLoadMore:(PullTableView*)pullTableView{
    
}

@end
